<?php
namespace App\Services\Game;
use App\DTOS\Admin\ActiveRuleSearchDTO;
use App\Models\ActiveRule;
use App\Models\ActiveRuleOlineqdTc;
use App\Models\ActiveRuleQd;
use App\Models\ActiveRuleTc;
use App\Models\Game\Server;


class ActiveRuleService
{
    /**
    * @var  \Illuminate\Database\Eloquent\Builder
    */
    private ActiveRule $model;
    private ActiveRuleQd $modelqd;
    private ActiveRuleTc $modeltc;
    private ActiveRuleOlineqdTc $modelqdtc;
    public function __construct()
    {
        $this->model = new ActiveRule;
        $this->modelqd = new ActiveRuleQd();
        $this->modeltc = new ActiveRuleTc();
        $this->modelqdtc = new ActiveRuleOlineqdTc();
    }

    public function paginate(ActiveRuleSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $model = $this->model;
        $name= $DTO->getName();
        $num = $DTO->getNum();
        $TypeId = $DTO->getTypeId();


        /**反馈类型**/
        if (!is_null($name)){
            $model = $model->where('NAME ', $name );
        }
        if (!is_null($num)){
            $model = $model->whereIn('num ', $num );
        }
        if (!is_null($TypeId)){
            $model = $model->where('TypeId ', $TypeId );
        }


        return $model->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    /**
    * @param  ActiveRuleSearchDTO $insertDTO
    * @return  int
    */
    public function insert(ActiveRuleSearchDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->model->insertGetId($data);
    }

    /**
    * @param  int $id
    * @param  ActiveRuleSearchDTO $updateDTO
    */
    public function updateById($id,ActiveRuleSearchDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        // 处理 serverGroup
        $serverGroup = $data['serverGroup'];

        if($serverGroup){
            // 将 serverGroup 转换为数组
            if (is_string($serverGroup)) {
                $serverGroupArray = explode(',', $serverGroup);
            } else {
                $serverGroupArray = [$serverGroup]; // 单个值
            }

            // 查询对应的 server_id，去重
            $serverIds = Server::query()
                ->whereIn('group_id', $serverGroupArray)
                ->pluck('server_id')
                ->unique() // 去重
                ->toArray();

            // 将 server_ids 转换为以 * 分隔的字符串
            $data['ServerList'] = implode(',', $serverIds);
        }


        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->model->where('id',$id)->update($data);
    }

    public function deleteById($id)
    {
        $this->model->where('id',$id)->delete();
    }

    public function deleteByIds($ids)
    {
        if (empty($ids)) return;
        $this->model->whereIn('id',$ids)->delete();
    }
    public function getById($id)
    {
        return $this->model->where('id',$id)->first();
    }

    //渠道

    public function paginateqd(ActiveRuleSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $modelqd = $this->model;
        $name= $DTO->getName();
        $num = $DTO->getNum();
        $TypeId = $DTO->getTypeId();


        /**反馈类型**/
        if (!is_null($name)){
            $modelqd = $modelqd->where('NAME ', $name );
        }
        if (!is_null($num)){
            $modelqd = $modelqd->whereIn('num ', $num );
        }
        if (!is_null($TypeId)){
            $modelqd = $modelqd->where('TypeId ', $TypeId );
        }


        return $modelqd->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    /**
     * @param  ActiveRuleSearchDTO $insertDTO
     * @return  int
     */
    public function insertqd(ActiveRuleSearchDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->modelqd->insertGetId($data);
    }

    /**
     * @param  int $id
     * @param  ActiveRuleSearchDTO $updateDTO
     */
    public function updateByIdqd($id,ActiveRuleSearchDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        // 处理 serverGroup
        $serverGroup = $data['serverGroup'];

        if($serverGroup){
            // 将 serverGroup 转换为数组
            if (is_string($serverGroup)) {
                $serverGroupArray = explode(',', $serverGroup);
            } else {
                $serverGroupArray = [$serverGroup]; // 单个值
            }

            // 查询对应的 server_id，去重
            $serverIds = Server::query()
                ->whereIn('group_id', $serverGroupArray)
                ->pluck('server_id')
                ->unique() // 去重
                ->toArray();

            // 将 server_ids 转换为以 * 分隔的字符串
            $data['ServerList'] = implode(',', $serverIds);
        }


        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->modelqd->where('id',$id)->update($data);
    }

    public function deleteByIdqd($id)
    {
        $this->modelqd->where('id',$id)->delete();
    }

    public function deleteByIdsqd($ids)
    {
        if (empty($ids)) return;
        $this->modelqd->whereIn('id',$ids)->delete();
    }
    public function getByIdqd($id)
    {
        return $this->modelqd->where('id',$id)->first();
    }


    //提测渠道


    public function paginateqdtc(ActiveRuleSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $modelqd = $this->modelqdtc;
        $name= $DTO->getName();
        $num = $DTO->getNum();
        $TypeId = $DTO->getTypeId();


        /**反馈类型**/
        if (!is_null($name)){
            $modelqd = $modelqd->where('NAME ', $name );
        }
        if (!is_null($num)){
            $modelqd = $modelqd->whereIn('num ', $num );
        }
        if (!is_null($TypeId)){
            $modelqd = $modelqd->where('TypeId ', $TypeId );
        }


        return $modelqd->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    /**
     * @param  ActiveRuleSearchDTO $insertDTO
     * @return  int
     */
    public function insertqdtc(ActiveRuleSearchDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->modelqdtc->insertGetId($data);
    }

    /**
     * @param  int $id
     * @param  ActiveRuleSearchDTO $updateDTO
     */
    public function updateByIdqdtc($id,ActiveRuleSearchDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        // 处理 serverGroup
        $serverGroup = $data['serverGroup'];

        if($serverGroup){
            // 将 serverGroup 转换为数组
            if (is_string($serverGroup)) {
                $serverGroupArray = explode(',', $serverGroup);
            } else {
                $serverGroupArray = [$serverGroup]; // 单个值
            }

            // 查询对应的 server_id，去重
            $serverIds = Server::query()
                ->whereIn('group_id', $serverGroupArray)
                ->pluck('server_id')
                ->unique() // 去重
                ->toArray();

            // 将 server_ids 转换为以 * 分隔的字符串
            $data['ServerList'] = implode(',', $serverIds);
        }


        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->modelqdtc->where('id',$id)->update($data);
    }

    public function deleteByIdqdtc($id)
    {
        $this->modelqdtc->where('id',$id)->delete();
    }

    public function deleteByIdsqdtc($ids)
    {
        if (empty($ids)) return;
        $this->modelqdtc->whereIn('id',$ids)->delete();
    }
    public function getByIdqdtc($id)
    {
        return $this->modelqdtc->where('id',$id)->first();
    }

    //官方提测

    public function paginatetc(ActiveRuleSearchDTO $DTO)
    {
        $perPage = $DTO->getLimit();
        $page = $DTO->getPage();
        $model = $this->modeltc;
        $name= $DTO->getName();
        $num = $DTO->getNum();
        $TypeId = $DTO->getTypeId();


        /**反馈类型**/
        if (!is_null($name)){
            $model = $model->where('NAME ', $name );
        }
        if (!is_null($num)){
            $model = $model->whereIn('num ', $num );
        }
        if (!is_null($TypeId)){
            $model = $model->where('TypeId ', $TypeId );
        }


        return $model->orderByDesc("id")->paginate($perPage,'*','page',$page);
    }

    /**
     * @param  ActiveRuleSearchDTO $insertDTO
     * @return  int
     */
    public function inserttc(ActiveRuleSearchDTO $insertDTO){
        $data = $insertDTO->toArray();
        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['created_at'] = now();
        unset($data['page']); unset($data['limit']);
        return $this->modeltc->insertGetId($data);
    }

    /**
     * @param  int $id
     * @param  ActiveRuleSearchDTO $updateDTO
     */
    public function updateByIdtc($id,ActiveRuleSearchDTO $updateDTO)
    {
        $data = $updateDTO->toArray();
        // 处理 serverGroup
        $serverGroup = $data['serverGroup'];

        if($serverGroup){
            // 将 serverGroup 转换为数组
            if (is_string($serverGroup)) {
                $serverGroupArray = explode(',', $serverGroup);
            } else {
                $serverGroupArray = [$serverGroup]; // 单个值
            }

            // 查询对应的 server_id，去重
            $serverIds = Server::query()
                ->whereIn('group_id', $serverGroupArray)
                ->pluck('server_id')
                ->unique() // 去重
                ->toArray();

            // 将 server_ids 转换为以 * 分隔的字符串
            $data['ServerList'] = implode(',', $serverIds);
        }


        foreach($data as &$value){
            if (is_array($value)){
                $value= json_encode(array_values($value));
            }
        }
        $data['updated_at'] = now();
        unset($data['page']); unset($data['limit']);
        $this->modeltc->where('id',$id)->update($data);
    }

    public function deleteByIdtc($id)
    {
        $this->modeltc->where('id',$id)->delete();
    }

    public function deleteByIdstc($ids)
    {
        if (empty($ids)) return;
        $this->modeltc->whereIn('id',$ids)->delete();
    }
    public function getByIdtc($id)
    {
        return $this->modeltc->where('id',$id)->first();
    }
}
